package com.xx.pmis.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.xx.pmis.model.Function;
import com.xx.pmis.model.Role;
import com.xx.pmis.model.User;
import com.xx.pmis.services.FunctionService;
import com.xx.pmis.services.RoleService;
import com.xx.pmis.services.UserService;
import com.xx.pmis.web.util.CurrentDate;

/**
 * @class 系统初始化 <br>
 * 
 * @editor 石星星  @Date 2011-12-30 上午09:48:41 <br>
 * @version jdk_1.5.0
 */
public class ApplicationInitialization {  

	private static final Logger logger = Logger.getLogger(ApplicationInitialization.class);
    private UserService userService; 
    private RoleService roleService;  
    private FunctionService functionService; 
    
    public ApplicationInitialization(UserService userService,
			RoleService roleService, FunctionService functionService) {
		super();
		this.userService = userService;
		this.roleService = roleService;
		this.functionService = functionService;
		try {
			init();
		} catch (Exception e) {
            logger.info("初始化异常！" + e.toString());
		}
	}

	public void init() throws Exception {
		 logger.info("初始化...");
         createRole();
    }
    
    /**
     * 初始化角色
     * 
     * @throws Exception
     */
    private void createRole() throws Exception {
		// 初始化角色
    	Role role1 = new Role();
    	role1.setRoleName("系统管理员");
    	role1.setRoleCode("200000");
    	role1.setRoleRegDate(CurrentDate.currentDate());
    	role1.setRoleRegDate(CurrentDate.currentDate());
    	role1.setRoleState("0");
    	role1.setRoleDesc("");
		roleService.saveOrUpdateRole(role1);
    	Role role2 = new Role();
    	role2.setRoleName("一般会员");
    	role2.setRoleCode("209999");
    	role2.setRoleRegDate(CurrentDate.currentDate());
    	role2.setRoleRegDate(CurrentDate.currentDate());
    	role2.setRoleState("0");
    	role2.setRoleDesc("");
		roleService.saveOrUpdateRole(role2);
		
		createAdminUser();
		
	}  
    
    /**
     * 初始化系统管理员
     * 
     * @throws Exception
     */
    private void createAdminUser() throws Exception {	
		// 初始化用户
		Role role = null;
		role = roleService.getRoleByRoleCode("200000");
		User user = new User();
		user.setUserName("admin");
		user.setUserCode("100000");
		user.setUserPassword("admin");
		user.setUserRegDate(CurrentDate.currentDate());
		user.setUserModDate(CurrentDate.currentDate());
		user.setUserState("0");
		user.setRole(role);
		userService.saveOrUpdateUser(user);
		
		createFunction();
		
	} 

    /**
     * 初始化系统菜单
     * 
     * @throws Exception
     */
    private void createFunction() throws Exception {
		// 初始化菜单
		Function function1 = new Function();
		function1.setFunctionID(1);
		function1.setFunctionParentID(-1);
		function1.setFunctioName("个人信息管理系统");
		function1.setFunctionState("0");
		functionService.saveOrUpdateFunction(function1);
		// 系统管理
		Function function10 = new Function();
		function10.setFunctionID(10);
		function10.setFunctionParentID(1);
		function10.setFunctioName("系统管理");
		function10.setFunctionState("0");
		functionService.saveOrUpdateFunction(function10);
		// 用户管理
		Function function101 = new Function();
		function101.setFunctionID(101);
		function101.setFunctionParentID(10);
		function101.setFunctioName("用户管理 | USER");
		function101.setFunctionURL("user.do?method=listUser");
		function101.setFunctionTarget("main");
		function101.setFunctionState("0");
		functionService.saveOrUpdateFunction(function101);
		// 角色管理
		Function function102 = new Function();
		function102.setFunctionID(102);
		function102.setFunctionParentID(10);
		function102.setFunctioName("角色管理 | ROLE");
		function102.setFunctionURL("role.do?method=listRole");
		function102.setFunctionTarget("main");
		function102.setFunctionState("0");
		functionService.saveOrUpdateFunction(function102);
		// 菜单管理
		Function function103 = new Function();
		function103.setFunctionID(103);
		function103.setFunctionParentID(10);
		function103.setFunctioName("菜单管理 | MENU");
		function103.setFunctionURL("function.do?method=listFunction");
		function103.setFunctionTarget("main");
		function103.setFunctionState("0");
		functionService.saveOrUpdateFunction(function103);
		
		createRightOfAdminUser();
		
	}  
    
    /**
     * 初始化管理员的权限
     * 
     * @throws Exception
     */
    private void createRightOfAdminUser() throws Exception {		
		// 初始化管理员权限
		List<Function> list = new ArrayList<Function>(0);
		// 一级目录
		List<?> list1 = functionService.getAllFunction();
		Iterator<?> it1 = list1.iterator();
		while(it1.hasNext()) {
			Function function = (Function) it1.next();
			list.add(function);
			
		}	
		// 二级目录系统管理
		List<?> list2 = functionService.getAllFunctionByFunctionParentID(10);
		Iterator<?> it2 = list2.iterator();
		while(it2.hasNext()) {
			Function function = (Function) it2.next();
			list.add(function);
		}
		Role r = roleService.getRoleByRoleCode("200000");
		r.setFunction(list);
		roleService.saveOrUpdateRole(r);
		
	}  
     
}
